﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ViewDataBusiness;
using System.Web.Script.Serialization;
using Tools;

namespace Frame.Controllers
{
    public class ViewSettingController : Controller
    {
        [HttpGet]
        public ActionResult SettingView(string ViewForder, string ViewName)
        {
            string UserID = SessionKey.CheckKeyThrow();
            AsynchronousLogin.CheckAsynchronousLoginThrow();
            ViewSetting viewSetting = ViewBusiness.LoadViewSetting(ViewForder, ViewName);
            viewSetting.UserID = UserID;
            viewSetting.Permissions = Request.QueryString["Permissions"];
            viewSetting.ViewForder = ViewForder;
            viewSetting.ViewName = ViewName;  
            viewSetting.UrlAction = AsynchronousLogin.GetAsynchronousLoginUrl(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("&AsynchronousLoginKey=")).Replace("ViewSetting/SettingView/", "ViewSetting/SettingSave/"), Request.QueryString["Permissions"]);
            viewSetting.UrlReferrer = Request.UrlReferrer.AbsoluteUri;
            viewSetting.JsonString = new JavaScriptSerializer().Serialize(viewSetting);
            ViewBag.FilePath = "/Views/" + ViewForder + "/" + ViewName + ".json";
            return View("/Views/ViewSetting/SettingView.cshtml", viewSetting);
        }

        [HttpGet]
        public ActionResult JsonSetting(string ViewForder, string ViewName)
        {
            string UserID = SessionKey.CheckKeyThrow();
            AsynchronousLogin.CheckAsynchronousLoginThrow();
            ViewBag.UrlAction = AsynchronousLogin.GetAsynchronousLoginUrl(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("&AsynchronousLoginKey=")).Replace("ViewSetting/JsonSetting/", "ViewSetting/JsonSave/"), Request.QueryString["Permissions"]);
            ViewBag.UrlReferrer = Request.UrlReferrer.AbsoluteUri;
            ViewBag.FilePath = "/Views/" + ViewForder + "/" + ViewName + ".json";
            ViewBag.JsonSetting = ViewBusiness.LoadJsonSetting(ViewForder, ViewName);
            return View("/Views/ViewSetting/JsonSetting.cshtml");
        }

        [HttpGet]
        public ActionResult SettingList(string ViewForder, string ViewName, string KeyA)
        {
            SessionKey.CheckKeyThrow();
            AsynchronousLogin.CheckAsynchronousLoginThrow();
            FileInfo[] Files = new DirectoryInfo(Server.MapPath("/Views/" + KeyA + "/")).GetFiles("*.json");
            return View("/Views/ViewSetting/SettingList.cshtml", Files);
        }

        [HttpGet]
        public ActionResult OnlineList(string ViewForder, string ViewName)
        {
            SessionKey.CheckKeyThrow();
            AsynchronousLogin.CheckAsynchronousLoginThrow();
            ViewBag.UrlAction = AsynchronousLogin.GetAsynchronousLoginUrl(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("&AsynchronousLoginKey=")).Replace("ViewSetting/OnlineList/", "ViewSetting/OnlineSave/"), Request.QueryString["Permissions"]);
            return View("/Views/ViewSetting/OnlineList.cshtml", HttpContext.Application["SessionDictionary"]);
        }

        [HttpPost]
        public ActionResult OnlineSave(string ViewForder, string ViewName)
        {
            try
            {
                SessionKey.CheckKeyThrow();
                AsynchronousLogin.CheckAsynchronousLoginThrow();
                switch (Request.Form["ActionType"])
                {
                    case "Lock":
                        Tools.SessionDictionary.SetSession(Request.Form["SessionID"], "Lock", true);
                        break;
                    case "UnLock":
                        Tools.SessionDictionary.SetSession(Request.Form["SessionID"], "Lock", false);
                        break;
                }
                return Json(new { Success = true, Message = "操作成功。" });
            }
            catch (Exception ex)
            {
                return Json(new { Success = false, Message = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult SettingSave(string ViewForder, string ViewName)
        {
            try
            {
                SessionKey.CheckKeyThrow();
                AsynchronousLogin.CheckAsynchronousLoginThrow();
                ViewSetting viewSetting = new JavaScriptSerializer().Deserialize<ViewSetting>(Request.Form["JsonString"]);
                ViewBusiness.SaveViewSetting(ViewForder, ViewName, viewSetting);
                return Json(new { Success = true, Message = "操作成功。" });
            }
            catch (Exception ex)
            {
                return Json(new { Success = false, Message = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult JsonSave(string ViewForder, string ViewName)
        {
            try
            {
                SessionKey.CheckKeyThrow();
                AsynchronousLogin.CheckAsynchronousLoginThrow();
                ViewBusiness.SaveJsonSetting(ViewForder, ViewName, Request.Form["JsonString"]);
                return Json(new { Success = true, Message = "操作成功。" });
            }
            catch (Exception ex)
            {
                return Json(new { Success = false, Message = ex.Message });
            }
        }
    }
}
